package com.hqyj.hrms.modules.account.controller;

import com.hqyj.hrms.modules.account.entity.User;
import com.hqyj.hrms.modules.account.service.UserRedisService;
import com.hqyj.hrms.modules.account.service.UserService;
import com.hqyj.hrms.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    UserRedisService redisService;

    @Autowired
    UserRedisService userRedisService;

    //系统登录
    @PostMapping("/loginPost")
    public HashMap<String, Object> loginPost(User user, HttpServletRequest request) {
        HttpSession session = request.getSession();
        System.out.println(session.getAttribute("info"));
        user.setUserPwd(MD5Util.getMD5(user.getUserPwd()));
        userService.login(user);
        return userService.login(user);
    }

    //用户注册
    @GetMapping("/register")
    public HashMap<String, Object> register(User user, HttpServletRequest request) {
        HttpSession session = request.getSession();
        System.out.println(session.getAttribute("info"));
        user.setUserPwd(MD5Util.getMD5(user.getUserPwd()));
        return userService.register(user);
    }

    //系统退出
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public HashMap<String, Object> logout() {
        HashMap<String, Object> map = new HashMap<String, Object>();
        userService.logout();
        map.put("info", "退出成功");
        return map;
    }

    //查询缓存
    @GetMapping("/selectRedis")
    public HashMap<String, Object> selectRedis(User user) {
        return redisService.selectRedis(user);
    }

    //redis发送邮箱验证码
    @PostMapping("/redisSend")
    public HashMap<String, Object> redisSendEmail(String email) {
        return userRedisService.emailRedis(email);
    }

    //redis邮箱登录
    @PostMapping("/redisLogin")
    public HashMap<String, Object> redisLogin(String code, String email) {
        return userRedisService.LoginEmailRedis(code, email);
    }

    //新增
    @GetMapping("/add")
    public HashMap<String, Object> add(User user) {
        user.setUserPwd(MD5Util.getMD5(user.getUserPwd()));
        return userService.add(user);
    }

    //修改
    @GetMapping("/update")
    public HashMap<String, Object> update(User user) {
        user.setUserPwd(MD5Util.getMD5(user.getUserPwd()));
        return userService.update(user);
    }

    //删除
    @GetMapping("/del")
    public HashMap<String, Object> del(Integer id) {
        return userService.del(id);
    }

    //查询
    @GetMapping("/list")
    public HashMap<String, Object> list(User user) {
        return userService.list(user);
    }

}

